/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.webbapps.cormac_susilo.register;

/**
 *
 * @author joel
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import edu.webbapps.cormac_susilo.core.IUserModel;


import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Named;
import javax.validation.constraints.*;

@Named("register")
@RequestScoped  // NOTE enterprise package, else disaster!!!
public class RegisterBB implements Serializable {

    @NotNull
    @Size(min = 2, message = "Enter a real name")
    private String username;
    @NotNull
    @Size(min = 2, message = "Enter a real name")
    private String firstname;
    @Size(min = 2, message = "Enter a real name")
    private String lastname;
    @Size(min = 2, message = "Enter a real email")
    private String email;
    @NotNull
    private String password;
    @NotNull
    @Size(min = 2, message = "Enter a real name")
    private String name;
    //@Digits(integer=2, fraction=0)
    @Min(value = 0, message = "Price must not be negative")
    private String price;
    @Inject
    IUserModel mgr;

    public String save() {
        // Id generated by database
        mgr.createUser(username, password, "",firstname, lastname, email);
        return "registerSuccess";  // TODO
    }
    
   
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    // Any name possible 
    public void actionListener(ActionEvent e) { // NOTE: faces.ActionEvent
        Logger.getAnonymousLogger().log(Level.INFO, "{0} actionListener", this);
    }

    // Any name possible
    public String action() {
        Logger.getAnonymousLogger().log(Level.INFO, "{0} action", this);

        return save();  // Where to go, navigate 
    }

    // Any name possible. NOTE: This is not AJAX will be called at submit
    public void valueChangeListener(ValueChangeEvent e) {
        Logger.getAnonymousLogger().log(Level.INFO, "{0} valueChangeListener", this);
    }
}
